<?php


namespace app\controller\admin;


use app\BaseController;
use app\common\OrderStatus;
use app\models\goods\CategoryModel;
use app\models\goods\ExchangeOderModel;
use app\models\goods\GoodsModel;
use app\models\goods\GoodsSkuModel;
use app\models\goods\PointsmallModel;
use app\models\goods\ProductModel;
use app\models\user\UserModel;
use app\services\user\TechnicianService;
use app\validate\GoodsValidate;
use think\facade\Db;

class IntegralGoodsController extends BaseAdminController
{
    function setSearch()
    {
        return [
            'like' => ['name'],
            '=' => ['is_del'],
        ];
    }

    function setSort()
    {
        return [
            'inviter_name' => 'inviter',
            'level_name' => 'level',
        ];
    }

    /**
     * 获取商品列表
     * @return \think\response\Json
     */
    public function getList()
    {
        try {
            $where = $this->searchWhere;
            $orderBy = $this->orderBy;
            $sort = $this->sort;
            $list = PointsmallModel::where($where)
                ->order($orderBy, $sort)
                ->paginate(['list_rows' => $this->limit, 'page' => $this->page])
                ->toArray();
            return $this->success($list);
        } catch (\Exception $exception) {
            Db::rollback();
            return $this->error($exception->getMessage());
        }
    }

    /**
     * 添加，编辑
     * @return \think\response\Json
     */
    public function add()
    {
        Db::startTrans();
        try {
            $data = $this->request->param();

            $model = new PointsmallModel();
            $model->save($data);
            Db::commit();
            return $this->success();
        } catch (\Exception $exception) {
            Db::rollback();
            return $this->error($exception->getMessage());
        }
    }

    public function edit()
    {
        Db::startTrans();
        try {
            $data = $this->request->except(['create_time', 'update_time']);

            $model = PointsmallModel::find($data['id']);
            if (!$model) {
                throw new \Exception('商品不存在');
            }
            $model->save($data);

            Db::commit();
            return $this->success();
        } catch (\Exception $exception) {
            Db::rollback();
            return $this->error($exception->getMessage());
        }
    }

    public function status()
    {
        Db::startTrans();
        try {
            $idArr = $this->request->param('id');
            $status = $this->request->param('status');
            if (is_array($idArr)) {
                foreach ($idArr as $id) {
                    $this->setGoodsStatus($id, $status);
                }
            } else {
                $this->setGoodsStatus($idArr, $status);
            }
            Db::commit();
            return $this->success();
        } catch (\Exception $exception) {
            Db::rollback();
            return $this->error($exception->getMessage());
        }
    }

    private function setGoodsStatus($id, $status)
    {
        try {
            $model = PointsmallModel::find($id);
            if (!$model) {
                throw new \Exception("非法请求");
            }
            if ($status == 2) {
                PointsmallModel::destroy($id);
            } else {
                $model->is_del = 1;
                $model->save();
            }
        } catch (\Exception $exception) {
            throw new \Exception($exception->getMessage());
        }
    }
}